home *** CD-ROM | disk | FTP | other *** search
- Path: tph100.physik.uni-leipzig.de!not-for-mail
- From: wfeldt@physik.uni-leipzig.de (Steffen Winterfeldt)
- Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
- Subject: Re: float != float and floats as return types
- Followup-To: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
- Date: 30 Jan 1996 18:21:39 GMT
- Organization: Uni Leipzig
- Message-ID: <4elnjj$er4@server2.rz.uni-leipzig.de>
- References: <4ej9lb$mpc@fu-berlin.de>
- NNTP-Posting-Host: tph100.physik.uni-leipzig.de
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- Axel Thimm (axl@zedat.fu-berlin.de) wrote:
- : Hello,
- : I am getting confused, about how C/C++ manage float binary operations,
- : in particular multiplication. The next C++ example gives me surprising
- : results:
- : *** cut here: begin file t_prec.cc
- : #include <iostream.h>
- : #include <iomanip.h>
- : #include <math.h>
- : float quad( float );
- : int main() {
- : for( int i=0; i<10; ++i ) {
- : float a, b, c;
- : a = i/13.123123;
- : b = a*a;
- : c = quad(a);
- : cout << (b - c) << '\t';
- : cout << (b - a*a) << '\t';
- : cout << (c - quad(a)) << '\n';
- : }
- : return 0;
- : }
- : float quad( float x ) { return x*x; }
- : [...]
-
- (c - quad(a)) is not zero, because quad's return value is in a floating point
- register and so has higher precision than c.
-
- BTW, with higher optimization (say, -O3), even the second column becomes zero.
-
-
- Steffen
-